//
//  PublicQuestionViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/4.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "PublicQuestionViewController.h"
#import "MoreAlertVC.h"
#import "PositionSelectViewController.h"
#import "JobModel.h"

@interface PublicQuestionViewController ()<UITextViewDelegate>

@property (nonatomic, strong) MoreAlertVC *moreAlertVC;
@property (nonatomic, strong) JobModel *selectJob;

@end

@implementation PublicQuestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    
    self.loadingView.backgroundColor = kStyleColor;
}

#pragma mark - 导航栏模块

- (void)setupNav {
    self.navigationItem.title = @"向所有人提问";
    
    UIButton *tipB = [[UIButton alloc] init];
    [tipB setImage:[UIImage imageNamed:@"ic_problem"] forState:UIControlStateNormal];
    [tipB setMj_size:tipB.currentImage.size];
    [tipB addTarget:self action:@selector(tipBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tipB];
    
    self.textView.placeholder = @"请描述您的问题，建议越具体越好。";
}

- (void)tipBtnClick {
    _moreAlertVC = [[MoreAlertVC alloc] initWithIndex:1];
    [[UIApplication sharedApplication].keyWindow addSubview:_moreAlertVC.view];
}

- (IBAction)releaseBtnClick:(id)sender {
    self.releaseBtn.selected = !self.releaseBtn.selected;
}

- (IBAction)questionCategoryBtnClick:(id)sender {
    PositionSelectViewController *psVC = [[PositionSelectViewController alloc] init];
    psVC.selectBlock = ^(JobModel *job) {
        _selectJob = job;
        self.questionCategoryL.text = job.jobName;
    };
    [self.navigationController pushViewController:psVC animated:YES];
}

- (IBAction)questionBtnClick:(id)sender {
    if (self.textView.text.length == 0) {
        [CommonUtils showToast:@"请描述您的问题"];
        return;
    }
    if (!self.moneyTF.text || self.moneyTF.text.length == 0) {
        [CommonUtils showToast:@"请输入悬赏金额"];
        return;
    }
    if (!self.selectJob) {
        [CommonUtils showToast:@"请选择问题类型"];
        return;
    }
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest releaseQuestionWithRelatedId:self.selectJob.jobId andQuestionType:@"QUESTION_TYPE_PUBLIC" andContent:self.textView.text andBonus:self.moneyTF.text andIsAnonymous:self.releaseBtn.selected ? @"YES" : @"NO" andCompanyId:self.companyId result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            [CommonUtils showToast:[result objectForKey:@"data"]];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}


#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    if (textView.markedTextRange == nil) {
        NSLog(@"text:%@", textView.text);
        if (textView.text.length > 500) {
            ALERT_VIEW(@"最多输入500个字");
            textView.text = [textView.text subString:0 length:500];
        }
        NSString *count = [NSString stringWithFormat:@"%3zd/500",textView.text.length];
        NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:count];
        //获取要调整颜色的文字位置,调整颜色
        [hintString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0X42CDD9) range:NSMakeRange(0, 3)];
        
        [hintString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0XA5B9C2) range:NSMakeRange(3, 4)];
        
        self.countL.attributedText=hintString;
    }
}

@end
